package com.szc.demo01.controller;

import com.szc.demo01.exception.NotBossException;
import com.szc.demo01.pojo.Emp;
import com.szc.demo01.pojo.Schdule;
import com.szc.demo01.pojo.User;
import com.szc.demo01.pojo.dto.SchduleAndEmpDto;
import com.szc.demo01.service.EmpService;
import com.szc.demo01.service.SchduleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.szc.demo01.controller.base.BaseController;

/**
 * 日程管理控制器
 * @Author: ShiZhiCheng
 * @Date: 2021/01/13 11:43:40
 */
@Controller
public class SchduleController extends BaseController{
    @Resource
    private SchduleService schduleService;

    @Resource
    EmpService empService;

    /**
     * 查询所有
     * @param model
     * @return
     */
    @RequestMapping("/schdulelist")
    public String schdulelist(Model model){
        List<SchduleAndEmpDto> list = schduleService.getall(getUserId());
        model.addAttribute("list",list);
        return "schdule/schdule";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete_schdule")
    public String delete(Integer id) throws NotBossException {
        assertUserIdHasPerm(4);
        this.schduleService.removeById(id);
        return "redirect:/schdulelist";
    }

    /**
     * 跳转到add界面
     * @return
     */
    @RequestMapping("toaddschdule")
    public String toadd(Model model) throws NotBossException {
        assertUserIdHasPerm(4);

        List<Emp> empList = empService.list();
        model.addAttribute("empList",empList);
        return "schdule/addschdule";
    }


    /**
     * 新增
     * @param s
     * @return
     */
    @RequestMapping("/addschdule")
    public String add(Schdule s) throws NotBossException {
        assertUserIdHasPerm(4);
        s.setCreateTime(new Date());
        Emp ename = empService.getById(s.getEId());
        s.setEname(ename.getEname());
        schduleService.save(s);
        return "redirect:/schdulelist";
    }


    /**
     * 确认完成日程
     * @param id
     * @return
     */
    @RequestMapping("/ensurefinish")
    public String ensurefinish(Integer id) throws NotBossException {
        if (schduleService.getById(id).getEId() != getUserId()){
            throw new NotBossException("不是你的日程");
        }
        Schdule s = this.schduleService.selectbyid(id);
        s.setIsFinish(1);
        this.schduleService.updateById(s);
        return "redirect:/schdulelist";
    }

    /**
     * 根据是否完成查看日程
     */
    @RequestMapping("/getbyfinish")
    public String getbyfinish(Integer isFinish,Model model){
        List<SchduleAndEmpDto> list = schduleService.getbyfinish(isFinish,getUserId());
        model.addAttribute("list",list);
        return "schdule/schdule";
    }

}
